package cn.fl.spl.service.impl;

import cn.cmm.base.framework.constant.BaseConstant;
import cn.cmm.base.framework.exception.BaseException;
import cn.cmm.base.framework.logger.eum.LogExceptionEnum;
import cn.cmm.base.framework.logger.eum.LogOperEnum;
import cn.cmm.base.framework.utils.BaseLogUtils;
import cn.fl.framework.base.context.CurrentThreadContext;
import cn.fl.spl.service.IAppStatsDataService;
import cn.fl.spl.transform.AppStatsDataVOTransform;
import cn.fl.spl.vo.AppStatsDataVO;
import fl.constant.facade.framework.constant.BusinessIdConstant;
import fl.mongo.facade.analysis.api.IAppStatsDataApiService;
import fl.mongo.facade.analysis.api.IAppStatsDataQueryApiService;
import fl.mongo.facade.analysis.condition.AppStatsDataCondition;
import fl.mongo.facade.analysis.dto.AppStatsDataDTO;
import fl.mongo.facade.base.page.PaginationVo;
import fl.spl.facade.framework.constant.SplSystemConstant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by jiangzhongge on 2018-3-2.
 * Time 11:35.
 * Class belongs to fl-ass-service.
 */
@Service
public class AppStatsDataServiceImpl implements IAppStatsDataService {

    @Autowired
    private IAppStatsDataApiService appStatsDataApiService ;

    @Autowired
    private IAppStatsDataQueryApiService appStatsDataQueryApiService ;

    @Override
    public List<AppStatsDataVO> getAppStatsDataList(AppStatsDataCondition condition) throws BaseException {
        if(null == condition){
            BaseLogUtils.newWebLogger("主键参数为空").setLogOperType(LogOperEnum.REQ)
                    .setSysCd(CurrentThreadContext.getCurrentSysCd())
                    .setChannelCd(SplSystemConstant.FL_WEB_CHL_CD)
                    .setBussinessId(BusinessIdConstant.LOG_SPL_MANAGER_CD)
                    .setWarning(false)
                    .warn(LogExceptionEnum.COMMON_CODE_0002);
            return null ;
        }
        return AppStatsDataVOTransform.toVOList(this.appStatsDataQueryApiService.getAppStatsDataList(condition));
    }

    @Override
    public int insertAppStatsData(AppStatsDataVO appStatsDataVO) throws BaseException {
        int result = 0 ;
        if(null == appStatsDataVO){
            BaseLogUtils.newWebLogger("主键参数为空").setLogOperType(LogOperEnum.REQ)
                    .setSysCd(CurrentThreadContext.getCurrentSysCd())
                    .setChannelCd(SplSystemConstant.FL_WEB_CHL_CD)
                    .setBussinessId(BusinessIdConstant.LOG_SPL_MANAGER_CD)
                    .setWarning(false)
                    .warn(LogExceptionEnum.COMMON_CODE_0002);
        }else{
            result = this.appStatsDataApiService.addAppStatsData(AppStatsDataVOTransform.toDTO(appStatsDataVO));
        }
        return result;
    }

    @Override
    public boolean modifyAppStatsData(AppStatsDataCondition condition, AppStatsDataVO appStatsDataVO) throws BaseException {
        boolean flag = false ;
        if(null == condition || null == appStatsDataVO){
            BaseLogUtils.newWebLogger("修改接口参数不能为空").setLogOperType(LogOperEnum.REQ)
                    .setSysCd(CurrentThreadContext.getCurrentSysCd())
                    .setChannelCd(SplSystemConstant.FL_WEB_CHL_CD)
                    .setBussinessId(BusinessIdConstant.LOG_SPL_MANAGER_CD)
                    .setWarning(false)
                    .warn(LogExceptionEnum.COMMON_CODE_0002);
        }else{
            flag = this.appStatsDataApiService.updateAppStatsData(condition, AppStatsDataVOTransform.toDTO(appStatsDataVO));
        }
        return flag;
    }

    @Override
    public boolean upsertAppStatsData(AppStatsDataCondition condition, AppStatsDataVO appStatsDataVO) throws BaseException {
        boolean flag = false ;
        if(null == condition || null == appStatsDataVO){
            BaseLogUtils.newWebLogger("更新插入接口参数不能为空").setLogOperType(LogOperEnum.REQ)
                    .setSysCd(CurrentThreadContext.getCurrentSysCd())
                    .setChannelCd(SplSystemConstant.FL_WEB_CHL_CD)
                    .setBussinessId(BusinessIdConstant.LOG_SPL_MANAGER_CD)
                    .setWarning(false)
                    .warn(LogExceptionEnum.COMMON_CODE_0002);
        }else{
            flag = this.appStatsDataApiService.upsertAppStatsData(condition, AppStatsDataVOTransform.toDTO(appStatsDataVO));
        }
        return flag;
    }

    @Override
    public AppStatsDataVO getOneAppStatsData(AppStatsDataCondition condition) throws BaseException {
        if(null == condition){
            BaseLogUtils.newWebLogger("查询数据接口参数不能为空").setLogOperType(LogOperEnum.REQ)
                    .setSysCd(CurrentThreadContext.getCurrentSysCd())
                    .setChannelCd(SplSystemConstant.FL_WEB_CHL_CD)
                    .setBussinessId(BusinessIdConstant.LOG_SPL_MANAGER_CD)
                    .setWarning(false)
                    .warn(LogExceptionEnum.COMMON_CODE_0002);
            return null ;
        }
        return AppStatsDataVOTransform.toVO(this.appStatsDataQueryApiService.getOneAppStatsData(condition));
    }

    @Override
    public PaginationVo getAppStatsDataListByPage(PaginationVo paginationVo) throws BaseException {
        if(null == paginationVo){
            BaseLogUtils.newWebLogger("分页查询数据接口参数不能为空").setLogOperType(LogOperEnum.REQ)
                    .setSysCd(CurrentThreadContext.getCurrentSysCd())
                    .setChannelCd(SplSystemConstant.FL_WEB_CHL_CD)
                    .setBussinessId(BusinessIdConstant.LOG_SPL_MANAGER_CD)
                    .setWarning(false)
                    .warn(LogExceptionEnum.COMMON_CODE_0002);
            return null ;
        }
        PaginationVo page = this.appStatsDataQueryApiService.findAppStatsDataListByPage(paginationVo);
        List<AppStatsDataDTO> datas = (List<AppStatsDataDTO>)page.getDatas();
        List<AppStatsDataVO> voList = AppStatsDataVOTransform.toVOList(datas);
			
        page.setDatas(voList);
        return page ;
    }

    @Override
    public Long statisAppStatsDataCount(AppStatsDataCondition condition,Integer isDeweight) throws BaseException {
        if(null == condition){
            BaseLogUtils.newWebLogger("查询数据统计参数不能为空").setLogOperType(LogOperEnum.REQ)
                    .setSysCd(CurrentThreadContext.getCurrentSysCd())
                    .setChannelCd(SplSystemConstant.FL_WEB_CHL_CD)
                    .setBussinessId(BusinessIdConstant.LOG_SPL_MANAGER_CD)
                    .setWarning(false)
                    .warn(LogExceptionEnum.COMMON_CODE_0002);
            return null ;
        }
        Long count = 0L ;
        if(isDeweight != null && isDeweight == BaseConstant.IS_YESNO_YES){
        	count = this.appStatsDataQueryApiService.queryDistinctStatsDataCount(condition, "dataId", BaseConstant.IS_YESNO_NO);
        }else{
        	count = this.appStatsDataQueryApiService.queryStoreStatsDataCount(condition);
        }
        
        return count ;
    }

    @Override
    public List<AppStatsDataVO> queryAggregationDataInfo(AppStatsDataCondition condition, String groupType, String groupParam, String aliasParam, String projectType, String sortType, String sortParam, String... fields) {
        return AppStatsDataVOTransform.toVOList(this.appStatsDataQueryApiService.queryAggregationDataInfo(condition,groupType,groupParam,aliasParam,projectType,sortType,sortParam,fields));
    }
    
}